//
//  YDOAProCahngeVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/5/29.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit

class YDOAProCahngeVC: UITableViewController {
        
    
    lazy var statusArr : Array  = Array<Int>() //保存所有头部的状态
    lazy var headerViews : Array = Array<UIView>() //保存所有头部视图
    
    
    lazy var headerView = { () -> UIView in
        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 30))
        let lab = UILabel.ym_lable(text: "申请人qq", fontSize: 15, color: UIColor.black)
        lab.frame = CGRect(x: 0, y: 0, width: ScreenWidth / 5, height: 30)
        lab.textAlignment = .center
        lab.isUserInteractionEnabled = true
        view1.addSubview(lab)
        let  lab1 = UILabel.ym_lable(text: "申请物品", fontSize: 15, color: UIColor.black)
        lab1.frame = CGRect(x: ScreenWidth / 5, y: 0, width: ScreenWidth / 5, height: 30)
        lab1.textAlignment = .center
        lab1.isUserInteractionEnabled = true
        view1.addSubview(lab1)
        let  lab2 = UILabel.ym_lable(text: "价格", fontSize: 15, color: UIColor.black)
        lab2.frame = CGRect(x: (ScreenWidth / 5) * 2, y: 0, width: ScreenWidth / 5, height: 30)
        lab2.textAlignment = .center
        lab2.isUserInteractionEnabled = true
        view1.addSubview(lab2)
        view1.backgroundColor = UIColor.lightGray
        return view1
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "固定资产变更"
        
        setUpHeaderView()
        
        self.navigationController?.navigationBar.isHidden = false
        tableView.contentInset =  UIEdgeInsetsMake(0, 0, 0, 0);
       
        tableView.backgroundView = UIImageView(image: UIImage(named: "timg"))
        
        
        
        tableView.register(  UINib(nibName: "YDOAProChangeCell", bundle: nil), forCellReuseIdentifier: "YDOAProChangeCell")
        
     
        
        for _ in 0..<10 {
            statusArr.append(0)
        }
        headerViews.removeAll()
        tableView.reloadData()
        
    }
    
    
    private func setUpHeaderView(){
        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 40))
        let lab = UILabel.ym_lable(text: "使用人", fontSize: 15, color: UIColor.black)
        lab.frame = CGRect(x: 0, y: 0, width: ScreenWidth / 5, height: 40)
        lab.textAlignment = .center
        lab.isUserInteractionEnabled = true
        view1.addSubview(lab)
        let  lab1 = UILabel.ym_lable(text: "使用物品", fontSize: 15, color: UIColor.black)
        lab1.frame = CGRect(x: ScreenWidth / 5, y: 0, width: ScreenWidth / 5, height: 40)
        lab1.textAlignment = .center
        lab1.isUserInteractionEnabled = true
        view1.addSubview(lab1)
        let  lab2 = UILabel.ym_lable(text: "所属部门", fontSize: 15, color: UIColor.black)
        lab2.frame = CGRect(x: (ScreenWidth / 5) * 2, y: 0, width: ScreenWidth / 5, height: 40)
        lab2.textAlignment = .center
        lab2.isUserInteractionEnabled = true
        view1.addSubview(lab2)
        
        let  lab3 = UILabel.ym_lable(text: "新旧程度", fontSize: 15, color: UIColor.black)
        lab3.frame = CGRect(x: (ScreenWidth / 5) * 3, y: 0, width: ScreenWidth / 5, height: 40)
        lab3.textAlignment = .center
        lab3.isUserInteractionEnabled = true
        view1.addSubview(lab3)
        
        
        let  lab4 = UILabel.ym_lable(text: "时间", fontSize: 15, color: UIColor.black)
        lab4.frame = CGRect(x: (ScreenWidth / 5) * 4, y: 0, width: ScreenWidth / 5, height: 40)
        lab4.textAlignment = .center
        lab4.isUserInteractionEnabled = true
        view1.addSubview(lab4)
        
        view1.backgroundColor = UIColor.lightGray
        let  lineView = UIView(frame: CGRect(x: 0, y: 39, width: ScreenWidth, height: 1))
        lineView.backgroundColor = UIColor.ym_colorWithRGB(r: 222, g: 222, b: 222)
        view1.addSubview(lineView)
        tableView.tableHeaderView = view1
        //tableView.h.addSubview(view1)
        
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return (statusArr.count)
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if statusArr[section] == 0 {
            return 0
        }
        return 1
    }
    
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        //删除所有视图
        
        
        let view1 = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 40))
        let lab = UILabel.ym_lable(text: "曾诚", fontSize: 15, color: UIColor.black)
        lab.frame = CGRect(x: 0, y: 0, width: ScreenWidth / 5, height: 40)
        lab.textAlignment = .center
        lab.isUserInteractionEnabled = true
        view1.addSubview(lab)
        let  lab1 = UILabel.ym_lable(text: "手机", fontSize: 15, color: UIColor.black)
        lab1.frame = CGRect(x: ScreenWidth / 5, y: 0, width: ScreenWidth / 5, height: 40)
        lab1.textAlignment = .center
        lab1.isUserInteractionEnabled = true
        view1.addSubview(lab1)
        let  lab2 = UILabel.ym_lable(text: "信息中心", fontSize: 15, color: UIColor.black)
        lab2.frame = CGRect(x: (ScreenWidth / 5) * 2, y: 0, width: ScreenWidth / 5, height: 40)
        lab2.textAlignment = .center
        lab2.isUserInteractionEnabled = true
        view1.addSubview(lab2)
        
        let  lab3 = UILabel.ym_lable(text: "全新", fontSize: 15, color: UIColor.black)
        lab3.frame = CGRect(x: (ScreenWidth / 5) * 3, y: 0, width: ScreenWidth / 5, height: 40)
        lab3.textAlignment = .center
        lab3.isUserInteractionEnabled = true
        view1.addSubview(lab3)
        
        
        let  lab4 = UILabel.ym_lable(text: "2017-4-29", fontSize: 15, color: UIColor.black)
        lab4.frame = CGRect(x: (ScreenWidth / 5) * 4, y: 0, width: ScreenWidth / 5, height: 40)
        lab4.textAlignment = .center
        lab4.adjustsFontSizeToFitWidth = true
        lab4.isUserInteractionEnabled = true
        view1.addSubview(lab4)
        
        view1.tag = 500 + section
        let tap = UITapGestureRecognizer(target: self, action: #selector(click))
        view1.addGestureRecognizer(tap)
        let  lineView = UIView(frame: CGRect(x: 0, y: 39, width: ScreenWidth, height: 1))
        lineView.backgroundColor = UIColor.ym_colorWithRGB(r: 222, g: 222, b: 222)
        view1.addSubview(lineView)
        
        view1.backgroundColor = UIColor.white
        //        button.addTarget(self, action: #selector(click), for: .touchUpInside)
        if headerViews.count < statusArr.count{
            headerViews.append(view1)
        }
        
        return view1
        
        
        
    }
    
    @objc private func click(tap : UIGestureRecognizer){
        let tag = (tap.view?.tag)! - 500
        if statusArr[tag] == 0 {
            statusArr[tag] = 1
        }else{
            statusArr[tag] = 0
        }
        headerViews.removeAll()
        tableView.reloadData()
    }
    
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
        
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "YDOAProChangeCell", for: indexPath) as? YDOAProChangeCell  else {
            return YDOAProChangeCell()
        }
        cell.backgroundColor = UIColor.clear
        //传递id
        cell.index = indexPath.section
        
        
//        cell.cellId = indexPath.section
        cell.selectionStyle = .none
        cell.callBack = {(type : Int, index : Int) in
            
            let vc =  YDOAProChangePresentVC()
            
            switch type {
            
            case 0:
                YMLog.log(item: "修改使用人\(index)")
                vc.type = 0
            case 1:
                YMLog.log(item: "修改新旧程度\(index)")
                vc.type = 1
            case 2:
                YMLog.log(item: "修改使用部门\(index)")
                vc.type = 2
            case 3:
                YMLog.log(item: "修改全部\(index)")
                vc.type = 3
            default:
                break
            }
            vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
            vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色

            self.present(vc, animated: true, completion: {
                
            })
            
            
        }
        
        
        return cell
    }
    
    
    
    
}
